﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HR.Appointments.Service;

public partial class DisplayAllOrders : EmployeePage
{
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckCredentials();
        _imgConfirm.Visible = IsLoggedIn;
        if (!IsPostBack)
        {
            ListAllOrders();
        }
    }

    /// <summary>
    /// Puts confirm attribute on GridView link buttons.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void _dtgAllorders_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Viltu Staðfesta
            LinkButton confirm = (LinkButton)e.Row.FindControl("_lnkConfirm");
            confirm.Attributes.Add("onclick", "javascript:return " +
            "confirm('Viltu staðfesta pöntun?')");

            //Viltu hafna
            LinkButton deny = (LinkButton)e.Row.FindControl("_lnkDeny");
            deny.Attributes.Add("onclick", "javascript:return " +
            "confirm('Viltu hafna pöntun?')");
        }
    }
    
    /// <summary>
    /// Handles denial of order from grid.  
    /// Marks the order as denied and then refreshes the list.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
	protected void _dtgAllorders_RowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		System.Collections.Specialized.OrderedDictionary keys = 
			(System.Collections.Specialized.OrderedDictionary)_dtgAllorders.DataKeys[e.RowIndex].Values;
		
		int companyId = Convert.ToInt32(keys["CompanyId"]);
		int orderId = Convert.ToInt32(keys["OrderId"]);

        try
        {
            Facade.Employee().DenyOrder(companyId, orderId);
        }
        catch (SubmitException)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að hafna pöntun.');", true);
        }

        ListAllOrders();
    }

    /// <summary>
    /// Handles the confirmation of the order from the grid.
    /// Marks the order as confirmed and then refreshes the list.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void _dtgAllorders_RowEditing(object sender, GridViewEditEventArgs e)
    {
        System.Collections.Specialized.OrderedDictionary keys =
            (System.Collections.Specialized.OrderedDictionary)_dtgAllorders.DataKeys[e.NewEditIndex].Values;

        int companyId = Convert.ToInt32(keys["CompanyId"]);
        int orderId = Convert.ToInt32(keys["OrderId"]);

        try
        {
            Facade.Employee().ConfirmOrder(companyId, orderId);
        }
        catch (SubmitException)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að samþykkja pöntun.');", true);
        }

        ListAllOrders();
    }

    /// <summary>
    /// Handles the button that send the notification to customers.
    /// It will send notification to all users who has been confirmed or denied,
    /// but leaves the unconfirmed in the grid.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void _btnNotifyCustomers_Click(object sender, EventArgs e)
    {
        try
        {
            Facade.Notify().SendNotifications(GetCompanyIdFromCookie());

            ListAllOrders();

            Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Áminningar hafa verið sendar.');", true);
        }
        catch (SubmitException)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að senda áminningar á allar pantanir.');", true);
        }
    }
    #endregion

    #region Link Transfer
    /// <summary>
    /// Link to main page
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void _ibmHomePage_Click(object sender, ImageClickEventArgs e)
	{
        if (!IsPostBack || (e.X != 0 && e.Y != 0))
        {
            Server.Transfer("~/Company.aspx",true);
        }
		
	}

    /// <summary>
    /// Link to employee home page
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
	protected void _lbtnCompanyBooking_Click(object sender, EventArgs e)
	{
		Server.Transfer("~/CompanyBooking.aspx",true);
    }

    protected void _lbtnConfig_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/Config.aspx", true);
    }
    #endregion

    #region Private List Methods
    /// <summary>
    /// Bind list of orders to grid
    /// </summary>
    protected void ListAllOrders()
    {
        _dtgAllorders.DataSource = Facade.Employee().GetAllOrders(
            GetCompanyIdFromCookie());
        _dtgAllorders.DataBind();
    }
    #endregion
    
}
